package com.nullpointer.playerinfo.bungee;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.nullpointer.playerinfo.common.Base;
import com.nullpointer.playerinfo.common.Methods;
import com.nullpointer.playerinfo.common.exception.PlayerInfoException;
import com.nullpointer.playerinfo.common.objects.Player;
import com.nullpointer.playerinfo.common.objects.PluginMode;

import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor;

public class Commands extends Command implements TabExecutor {

    public Commands() {
        super("playerinfo", "playerinfo.admin", "pi");
    }

    @Override
    public void execute(CommandSender sender, String[] args) {
        if (args.length == 2) {
            switch (args[0].toLowerCase()) {
                case "getfirstplaytime":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {
                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getFirstPlayTime())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }
                        }

                    });

                    break;

                case "getlastplaytime":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getLastPlayTime())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getfirstplaytimeformatted":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(
                                        new TextComponent(String.valueOf(player.getFirstPlayTimeFormatted())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));

                            }

                        }

                    });
                    break;

                case "getlastplaytimeformatted":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(
                                        new TextComponent(String.valueOf(player.getLastPlayTimeFormatted())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getcash":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getCash())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getexp":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getExp())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getlevel":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getLevel())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getonlinetime":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getOnlineTime())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "gettotalonlinetime":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getTotalOnlineTime())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getonlinetimefomatted":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getOnlineTimeFomatted())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "gettotalonlinetimefomatted":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(
                                        new TextComponent(String.valueOf(player.getTotalOnlineTimeFomatted())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "getupgradeexp":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                sender.sendMessage(new TextComponent(String.valueOf(player.getUpgradeExp())));
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            }

                        }

                    });
                    break;

                case "removedata":
                    PlayerInfoBungee.runTaskAsynchronously(new Runnable() {

                        @Override
                        public void run() {
                            Player player;
                            try {
                                player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                                player.removeData();
                                sender.sendMessage(new TextComponent("已将玩家" + player.getName() + "的数据抹除！！"));
                                if (Base.hotUpdate) {
                                    PlayerInfoBungee.mysql.savePlayer(player);
                                }
                                PlayerInfoBungee.corresponder.sendMessage("removedata " + args[1]);
                            } catch (PlayerInfoException e) {
                                sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }

                        }

                    });
                    break;

                default:
                    sender.sendMessage(new TextComponent("语法错误！！！"));
                    break;
            }

        } else if (args.length == 3) {
            switch (args[0].toLowerCase()) {
                case "addexp":
                    PlayerInfoBungee.runTaskAsynchronously(() -> {
                        Player player;
                        try {
                            player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                            int exp = Integer.parseInt(args[2]);
                            player.addExp(exp);
                            sender.sendMessage(new TextComponent("为玩家" + player.getName() + "增加" + exp + "点经验值！！！"));
                            if (Base.hotUpdate) {
                                PlayerInfoBungee.mysql.savePlayer(player);
                            }
                            PlayerInfoBungee.corresponder.sendMessage("addexp " + args[1] + " " + args[2]);
                        } catch (PlayerInfoException e) {
                            sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                        } catch (NumberFormatException e) {
                            sender.sendMessage(new TextComponent("参数错误！！！"));
                            return;
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    });
                    break;

                case "removeexp":
                    PlayerInfoBungee.runTaskAsynchronously(() -> {
                        Player player;
                        try {
                            player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                            int exp = Integer.parseInt(args[2]);
                            player.removeExp(exp);
                            sender.sendMessage(new TextComponent("扣除玩家" + player.getName() + exp + "点经验值！！！"));
                            if (Base.hotUpdate) {
                                PlayerInfoBungee.mysql.savePlayer(player);
                            }
                            PlayerInfoBungee.corresponder.sendMessage("removeexp " + args[1] + " " + args[2]);
                        } catch (PlayerInfoException e) {
                            sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                        } catch (NumberFormatException e) {
                            sender.sendMessage(new TextComponent("参数错误！！！"));
                            return;
                        } catch (SQLException e) {
							e.printStackTrace();
						}
                    });
                    break;
                
                case "addcash":
                    PlayerInfoBungee.runTaskAsynchronously(() -> {
                        Player player;
                        try {
                            player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                            int cash = Integer.parseInt(args[2]);
                            sender.sendMessage(new TextComponent("玩家" + player.getName() + "当前金币：" + player.getCash()));
                            sender.sendMessage(new TextComponent("增加中..."));
                            player.addCash(cash);
                            sender.sendMessage(new TextComponent("增加成功！玩家" + player.getName() + "当前金币：" + player.getCash()));
                            if (Base.hotUpdate) {
                                PlayerInfoBungee.mysql.savePlayer(player);
                            }
                            PlayerInfoBungee.corresponder.sendMessage("addcash " + args[1] + " " + args[2]);
                        } catch (PlayerInfoException e) {
                            sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                        } catch (NumberFormatException e) {
                            sender.sendMessage(new TextComponent("参数错误！！！"));
                            e.printStackTrace();
                            return;
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    });
                    break;

                case "removecash":
                    PlayerInfoBungee.runTaskAsynchronously(() -> {
                        Player player;
                        try {
                            player = Methods.getCmdTarget(args[1], PluginMode.BUNGEE);
                            int cash = Integer.parseInt(args[2]);
                            sender.sendMessage(new TextComponent("玩家" + player.getName() + "当前金币：" + player.getCash()));
                            sender.sendMessage(new TextComponent("扣除中..."));
                            player.removeCash(cash);
                            sender.sendMessage(new TextComponent("扣除成功！玩家" + player.getName() + "当前金币：" + player.getCash()));
                            if (Base.hotUpdate) {
                                PlayerInfoBungee.mysql.savePlayer(player);
                            }
                            PlayerInfoBungee.corresponder.sendMessage("removecash " + args[1] + " " + args[2]);
                        } catch (PlayerInfoException e) {
                            sender.sendMessage(new TextComponent("找不到该玩家！！！"));
                        } catch (NumberFormatException e) {
                            sender.sendMessage(new TextComponent("参数错误！！！"));
                            e.printStackTrace();
                            return;
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    });
                    break;
                default:
                    sender.sendMessage(new TextComponent("语法错误！！！"));
                    break;
            }
        }
    }

    @Override
    public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
        
        if (args.length == 1) {
            List<String> result = new ArrayList<>();
            if (sender.hasPermission("playerinfo.admin")) {
                result.addAll(new ArrayList<>(Arrays.asList("getfirstplaytime", "getlastplaytime", "getfirstplaytimeformatted", "getlastplaytimeformatted",
                "getcash", "getexp", "getcash", "getlevel", "getonlinetime", "gettotalonlinetime", "getonlinetimefomatted", "gettotalonlinetimefomatted",
                "getupgradeexp", "addexp", "addcash", "removecash", "removedata")));
            }
            result.removeIf(item -> !item.startsWith(args[0].toLowerCase()));
            return result;
        } else if (args.length == 2) {
            List<String> result = new ArrayList<>();
            for (Player p : Base.playerMap.values()) {
                result.add(p.getName());
            }
            result.removeIf(item -> !item.startsWith(args[1]));
            return result;
        }
        return Collections.emptyList();
    }

    
}
